package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.zfei.gfb.api.StatsService;
import com.zfei.gfb.api.domain.StatsTag;
import com.zfei.gfb.api.domain.StatsValue;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbLandResourcesMapper;
import com.zfei.gfb.model.GfbLandPublicity;
import com.zfei.gfb.model.GfbLandResources;
import com.zfei.gfb.query.GfbLandResourcesQuery;
import com.zfei.gfb.query.h5.H5LandResourcesQuery;
import com.zfei.gfb.request.GfbDelRequest;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.request.GfbLandResourcesAddRequest;
import com.zfei.gfb.request.GfbLandResourcesEditRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.gson.Types;
import com.zfei.gfb.vo.GfbLandPublicityVo;
import com.zfei.gfb.vo.GfbLandResourcesListVo;
import com.zfei.gfb.vo.GfbLandResourcesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @author
 */
@Api(tags = "H5土地，池塘，山林【招标】接口")
@RestController
@RequestMapping("/h5/land/resources")
public class H5LandResourcesController {

	@Resource
	private GfbLandResourcesMapper gfbLandResourcesMapper;

	@Resource
	private StatsService statsService;

	@ApiOperation(value = "列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = GfbLandResourcesListVo.class),
	})
	@LogResult
	@HttpJson("/list")
	public Object list(@RequestBody H5LandResourcesQuery query) {
		query.check().build(Now::millis);

		GfbLandResourcesListVo result = new GfbLandResourcesListVo();

		int total = gfbLandResourcesMapper.totalH5(query);
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}
		
		List<GfbLandResources> list = gfbLandResourcesMapper.listH5(query);

		//list image
		List<GfbLandResourcesVo> vos = new ArrayList<>();
		for (GfbLandResources item : list) {
			GfbLandResourcesVo vo = Beans.copy(item, GfbLandResourcesVo.class);
			List<String> images =  Gsons.fromJson(item.getImages(), Types.StringListType);

			switch (item.getType()){
				case Const.LAND:
					vo.setBrowseNum(statsService.get(item.getId().toString(), StatsTag.LAND_RESOURCE_BROWSE_COUNT).getNum());
					break;
				case Const.POOL:
					vo.setBrowseNum(statsService.get(item.getId().toString(), StatsTag.POND_RESOURCE_BROWSE_COUNT).getNum());
					break;
				case Const.FOREST:
					vo.setBrowseNum(statsService.get(item.getId().toString(), StatsTag.MOUNTAIN_RESOURCE_BROWSE_COUNT).getNum());
					break;
				default:
					break;
			}

			vo.setImages(images);
			vos.add(vo);
		}

		result.setData(vos);
		return result;

	}

	@ApiOperation(value = "招标详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbLandResourcesVo.class),
	})
	@LogResult
	@HttpJson("/get")
	public Object get(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbLandResources record = gfbLandResourcesMapper.selectByPrimaryKey(id);

		GfbLandResourcesVo vo = Beans.copy(record, GfbLandResourcesVo.class);
		List<String> images =  Gsons.fromJson(record.getImages(), Types.StringListType);
		vo.setImages(images);

		StatsValue postStats;
		switch (record.getType()){
			case Const.LAND:
				postStats = new StatsValue(record.getId().toString(), StatsTag.LAND_RESOURCE_BROWSE_COUNT, 1);
				statsService.inc(postStats);
				vo.setBrowseNum(statsService.get(record.getId().toString(), StatsTag.LAND_RESOURCE_BROWSE_COUNT).getNum());
				break;
			case Const.POOL:
				postStats = new StatsValue(record.getId().toString(), StatsTag.POND_RESOURCE_BROWSE_COUNT, 1);
				statsService.inc(postStats);
				vo.setBrowseNum(statsService.get(record.getId().toString(), StatsTag.POND_RESOURCE_BROWSE_COUNT).getNum());
				break;
			case Const.FOREST:
				postStats = new StatsValue(record.getId().toString(), StatsTag.MOUNTAIN_RESOURCE_BROWSE_COUNT, 1);
				statsService.inc(postStats);
				vo.setBrowseNum(statsService.get(record.getId().toString(), StatsTag.MOUNTAIN_RESOURCE_BROWSE_COUNT).getNum());
				break;
			default:
				break;
		}
		return vo;
	}
}
